"""
可视化部分：
    1. 迷宫可视化创建
    2. BOSS对决页面的可视化创建
"""
import pygame
from config import *
import numpy as np
import json

class MazeVisualizer:
    """
    游戏部分的可视化页面
    """
    def __init__(self, maze_file):
        """
        :param maze_file: 生成的迷宫
        """
        with open(maze_file, 'r') as f:
            self.maze_grid = np.array(json.load(f))
        self.size = len(self.maze_grid) # 从数据直接获取尺寸
        self.SCREEN_WIDTH = self.size * CELL_SIZE
        self.SCREEN_HEIGHT = self.size * CELL_SIZE

        self.screen = pygame.display.set_mode((self.SCREEN_WIDTH, self.SCREEN_HEIGHT))
        pygame.init()
        pygame.display.set_caption('算法驱动的迷宫探险')

        self.colors = {
            WALL: COLOR_WALL,
            PATH: COLOR_PATH,
            START: COLOR_START,
            EXIT: COLOR_EXIT,
            GOLD: COLOR_GOLD,
            TRAP: COLOR_TRAP,
            BOSS: COLOR_BOSS,
            LOCKER: COLOR_LOCKER,
        }

    # BOSS界面屏幕恢复
    def recover_size(self):
        self.screen = pygame.display.set_mode((self.SCREEN_WIDTH, self.SCREEN_HEIGHT))

    def draw_maze(self):
        self.screen.fill(COLOR_PATH)
        for r, row in enumerate(self.maze_grid):
            for c, cell in enumerate(row):
                color = self.colors.get(cell, COLOR_PATH)  # 如果找不到符号，默认为通路
                pygame.draw.rect(self.screen, color, (c * CELL_SIZE, r * CELL_SIZE, CELL_SIZE, CELL_SIZE))

    def draw_player(self, player_pos):
        r, c = player_pos
        pygame.draw.rect(self.screen, COLOR_PLAYER, (c * CELL_SIZE, r * CELL_SIZE, CELL_SIZE, CELL_SIZE))

    def update_display(self):
        pygame.display.flip()

    def draw_path(self, path, color):
        """在迷宫上高亮一条路径"""
        for r, c in path:
            # 在格子中间画一个小一点的圆圈，避免完全覆盖元素本身
            center_x = c * CELL_SIZE + CELL_SIZE // 2
            center_y = r * CELL_SIZE + CELL_SIZE // 2
            pygame.draw.circle(self.screen, color, (center_x, center_y), CELL_SIZE // 4)


if __name__ == '__main__':
    # 测试
    maze_visualizer = MazeVisualizer("data/maze.json")
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

        maze_visualizer.draw_maze()
        # 放置玩家
        # maze_visualizer.draw_player((CELL_SIZE, CELL_SIZE))
        maze_visualizer.update_display()
